// services/category.js
import { ipcMain } from 'electron'
import {
  getCategoryTree,
  getSubCategories,
  addCategory,
  updateCategory,
  deleteCategory
} from '../dao/category.js'

const MODEL_NAME = 'category'

export function init() {
  // 获取分类树
  ipcMain.handle(`${MODEL_NAME}/getTree`, async () => {
    return await getCategoryTree()
  })

  // 获取子分类
  ipcMain.handle(`${MODEL_NAME}/getSubCategories`, (event, parentId) => {
    return getSubCategories(parentId)
  })

  // 添加分类
  ipcMain.handle(`${MODEL_NAME}/add`, (event, name, parentId, level) => {
    return addCategory(name, parentId, level)
  })

  // 更新分类
  ipcMain.handle(`${MODEL_NAME}/update`, (event, id, data) => {
    return updateCategory(id, data)
  })

  // 删除分类
  ipcMain.handle(`${MODEL_NAME}/delete`, (event, id) => {
    return deleteCategory(id)
  })

  // 获取分类
  ipcMain.handle(`${MODEL_NAME}/tree`, () => {
    return getCategoryTree()
  })

}
